using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using RNChessBoardCommonTypes;

//---------------------------------------------------------------------------//
//																			 //
//			r  n  C  h  e  s  s  B  o  a  r  d  C  o  n  t  r  o  l			 //
//			=======================================================			 //
//						  (c) 2003 Chris R. Chapman							 //
//																			 //
//	Version:		1.0	Lite												 //
//  Date:			May 2003												 //
//  Author:			Chris R. Chapman (chris@chapmanconsulting.ca)			 //
//  Description:	A chessboard GUI written in 100% .Net/C#, featuring		 //
//					a resizable board with adjustable colors and configurable//
//					images for pieces, as well as events for capturing player//
//					movement and illegal moves.								 //
//																			 //
//					NOTE:  This GUI is not coupled to a rules engine to parse//
//						   moves and check their legality or enforce check,  //
//						   etc.  This will be featured in an upcoming version//
//						   later this year.									 //
//																			 //
// LICENSING:		Can be freely distributed and used as long as the		 //
//					following criteria are met:								 //
//																			 //
//					1) All copyright notices are kept intact;				 //
//					2) Product is not for commercial resale;				 //
//					3) Product is NOT used in conjunction with ANY GPLed or  //
//					   FSF software.  I cannot abide by their philosophies.  //
//					4) Credit for the control to me is made in some			 //
//					   conspicuous fashion, like an About box.				 //
//---------------------------------------------------------------------------//
namespace RNChessBoard
{
	/// <summary>
	/// Enumeration for describing a chess piece's side.
	/// </summary>
	public enum ChessPieceColor
	{
		Light = 1,
		Dark = 2
	}

	/// <summary>
	/// Base class for defining a chess piece set
	/// </summary>
	public class ChessPieceSet
	{
		public Image Pawn = null;
		public Image Knight = null;
		public Image Bishop = null;
		public Image Rook = null;
		public Image Queen = null;
		public Image King = null;
		private ChessPieceRectangle _chessPieceRect = new ChessPieceRectangle();
		private int _defaultPieceHeight = 32;
		private int _defaultPieceWidth = 32;
		private Image _masterImage = null;
		
		/// <summary>
		/// Default Constructor
		/// </summary>
		public ChessPieceSet()
		{}
		
		/// <summary>
		/// Loads chess piece images into object from master 32 x 192 bitmap
		/// </summary>
		/// <param name="MasterImage"></param>
		public void LoadImages(Image MasterImage)
		{
			_masterImage = MasterImage;

			// Six piece types...
			for(int i=ChessPieceDefinition.PAWN;i<=ChessPieceDefinition.KING;i++)
			{
				switch(i)
				{
					// Pawn
					case ChessPieceDefinition.PAWN:
						LoadImage(ref this.Pawn,this._chessPieceRect.Pawn);
						break;

					// Knight
					case ChessPieceDefinition.KNIGHT:
						LoadImage(ref this.Knight,this._chessPieceRect.Knight);
						break;

					// Bishop
					case ChessPieceDefinition.BISHOP:
						LoadImage(ref this.Bishop,this._chessPieceRect.Bishop);
						break;

					// Rook
					case ChessPieceDefinition.ROOK:
						LoadImage(ref this.Rook,this._chessPieceRect.Rook);
						break;

					// Queen
					case ChessPieceDefinition.QUEEN:
						LoadImage(ref this.Queen,this._chessPieceRect.Queen);
						break;

					// King
					case ChessPieceDefinition.KING:
						LoadImage(ref this.King,this._chessPieceRect.King);
						break;
				}
			}
		}


		/// <summary>
		/// Loads requisite image into the object from a given bounding rectangle.
		/// </summary>
		/// <param name="chessPieceImage"></param>
		/// <param name="pieceRect"></param>
		protected void LoadImage(ref Image chessPieceImage, Rectangle pieceRect)
		{
			Bitmap pieceBitmap = new Bitmap(this._defaultPieceWidth,this._defaultPieceHeight,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			Graphics pieceDC = Graphics.FromImage(pieceBitmap);
			Rectangle destRect = new Rectangle(0,0,this._defaultPieceWidth,this._defaultPieceHeight);

			pieceDC.DrawImage(_masterImage,destRect,pieceRect,System.Drawing.GraphicsUnit.Pixel);
			chessPieceImage = pieceBitmap;

			pieceDC.Dispose();
			
		}

		
		/// <summary>
		/// Determines if a given chess piece image exists in the set
		/// </summary>
		/// <param name="imageToFind"></param>
		/// <returns></returns>
		public bool ChessPieceImageExists(Image imageToFind)
		{
			if(imageToFind==this.Pawn ||
				imageToFind==this.Knight ||
				imageToFind==this.Bishop ||
				imageToFind==this.Rook ||
				imageToFind==this.Queen ||
				imageToFind==this.King)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// Gets chess piece image from given VCPieceTypes definition
		/// </summary>
		/// <param name="chessPieceType">Corresponds to values for chesspieces as defined by the
		/// ChessPieceDefinition class.  This parameter is color-blind.</param>
		/// <returns>Image object representing the requested piece, or null if none exists.</returns>
		public Image GetImageFromPieceType(int chessPieceType)
		{
			chessPieceType = Math.Abs(chessPieceType);
			if(chessPieceType==ChessPieceDefinition.PAWN)
			{
				return this.Pawn;
			}
			else if(chessPieceType==ChessPieceDefinition.KNIGHT)
			{
				return this.Knight;
			}
			else if(chessPieceType==ChessPieceDefinition.BISHOP)
			{
				return this.Bishop;
			}
			else if(chessPieceType==ChessPieceDefinition.ROOK)
			{
				return this.Rook;
			}
			else if(chessPieceType==ChessPieceDefinition.QUEEN)
			{
				return this.Queen;
			}
			else if(chessPieceType==ChessPieceDefinition.KING)
			{
				return this.King;
			}
			else
			{
				return null;
			}	
		}
	}


	/// <summary>
	/// Class for providing rectangle coordinates to extract relevant piece images from master
	/// bitmap image
	/// </summary>
	public class ChessPieceRectangle
	{
		public Rectangle Pawn = new Rectangle(0,0,32,32);
		public Rectangle Knight = new Rectangle(33,0,32,32);
		public Rectangle Bishop = new Rectangle(65,0,32,32);
		public Rectangle Rook = new Rectangle(97,0,32,32);
		public Rectangle Queen = new Rectangle(129,0,32,32);
		public Rectangle King = new Rectangle(161,0,32,32);

		/// <summary>
		/// Default Constructor
		/// </summary>
		public ChessPieceRectangle()
		{}
	}
}
